package top.kaoshanji.p2ch19a;

import top.kaoshanji.p2ch19a.support.Worker;

import java.util.concurrent.CountDownLatch;

/**
 * 代码清单-p2ch19a-532页a：使用CountDownLatch实现主从协作场景
 * 主线程需要等待子线程的结果，所以，主线程去等待
 * author kaoshanji
 * ^_^
 * create time 2025/2/27 19:40
 */
public class MasterWorkerDemo {

    public static void main(String[] args) throws InterruptedException {

        int workerNum = 100;

        // 共享的协作变量
        CountDownLatch latch = new CountDownLatch(workerNum);

        Worker[] workers = new Worker[workerNum];
        for (int i = 0; i < workerNum; i++) {
            workers[i] = new Worker(latch);
            workers[i].start();
        }

        latch.await();

        System.out.println("collect worker results");
    }

}
